﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LAB.Manager
{
    public static class FinderManager
    {
        public delegate void handler(t_finder finder);

        public static event handler afterInsert;
        public static event handler afterDelete;
        public static event handler afterEdit;

        //      name		搜索器名
        //owner		拥有者
        //rank

        static FinderManager()
        {

            // finder被删除之后,对finderTag的影响
            FinderManager.afterDelete += (finder) =>
            {
                using (var db = new TagerEntities())
                {
                    var list = db.t_finderTag.Where(c => c.finderId == finder.id);
                    foreach (var li in list)
                    {
                        db.t_finderTag.Remove(li);
                    }
                    db.SaveChanges();
                }
            };

        }


        public static int insert(string name, List<int> tagIds, string owner)
        {
            int rst = -1;
            using (var db = new TagerEntities())
            {

                if (db.t_finder.Count(c => c.name == name && c.owner == owner) > 0)
                {
                    return -2;
                }

                if (db.t_finder.Count(c => c.owner == owner) >= 5)
                {
                    return -3;
                }

                var rank = db.t_finder.Max(c => c.rank);
                var finder = new t_finder { name = name, owner = owner, rank = rank == null ? 0 : rank + 1 };
                db.t_finder.Add(finder);
                db.SaveChanges();
                foreach (var fti in tagIds.Select(c => new t_finderTag { finderId = finder.id, tagId = c }))
                {
                    db.t_finderTag.Add(fti);
                }
                db.SaveChanges();
                rst = 0;
                // trigger
                if (afterInsert != null)
                {
                    afterInsert(finder);
                }
            }
            return rst;
        }


        public static int edit(int id, string name, List<int> tagIds, string owner)
        {
            int rst = -1;
            using (var db = new TagerEntities())
            {
                var finder = db.t_finder.FirstOrDefault(c => c.id == id);
                if (finder == null)
                {
                    return -2;
                }
                if (!(finder.owner == owner || SysUtil.isSysEditor(owner)))
                {
                    return -3;
                }
                if (!string.IsNullOrEmpty(name))
                {
                    finder.name = name;
                }
                if (tagIds != null)
                {
                    var finderTags = db.t_finderTag.Where(c => c.finderId == id);
                    foreach (var fti in finderTags)
                    {
                        db.t_finderTag.Remove(fti);
                    }
                    foreach (var ti in tagIds)
                    {
                        db.t_finderTag.Add(new t_finderTag { finderId = id, tagId = ti });
                    }
                }
                db.SaveChanges();
                rst = 0;
                // trigger
                if (afterEdit != null)
                {
                    afterEdit(finder);
                }

            }
            return rst;
        }



        public static int delete(int id, string owner)
        {
            var rst = -1;
            using (var db = new TagerEntities())
            {
                var finder = db.t_finder.FirstOrDefault(c => c.id == id);
                if (finder == null)
                {
                    return -2;
                }
                if (finder.owner == owner || SysUtil.isSysEditor(owner))
                {
                    return -3;
                }
                db.t_finder.Remove(finder);

                // 删除finder的tag
                var finderTags = db.t_finderTag.Where(c => c.finderId == id);
                foreach (var fti in finderTags)
                {
                    db.t_finderTag.Remove(fti);
                }
                db.SaveChanges();
                rst = 0;
                // trigger
                if (afterDelete != null)
                {
                    afterDelete(finder);

                }
            }
            return rst;
        }


        public static List<t_finder> search(string owner)
        {
            List<t_finder> rst = null;
            using (var db = new TagerEntities())
            {
                rst = db.t_finder.Where(c => c.owner == owner).ToList();
            }
            return rst;
        }


        public static bool rank(List<int> ids, List<int> ranks, string owner)
        {
            var rst = false;
            using (var db = new TagerEntities())
            {
                for (int i = 0; i < ids.Count(); i++)
                {
                    var id = ids[i];
                    var finder = db.t_finder.FirstOrDefault(c => c.id == id);
                    if (finder != null)
                    {
                        finder.rank = ranks[i];
                    }
                }
                db.SaveChanges();
                rst = true;
            }
            return rst;
        }





        public static List<v_finder> search(string userLike, int status, int pageIndex, int pageSize, out int total)
        {
            List<v_finder> rst = null;
            using (var db = new TagerEntities())
            {

                IEnumerable<v_finder> list = db.v_finder;
                if (!string.IsNullOrEmpty(userLike))
                {
                    list = list.Where(c => c.owner.IndexOf(userLike) != -1);
                }
                if (status != -1)
                {

                    list = list.Where(c => c.status == status);
                }

                var listIds = list.GroupBy(c => c.id).Select(c => c.Key).Skip(pageIndex * pageSize).Take(pageSize).ToList();


                list = list.Where(c => listIds.Any(d => d == c.id));
                total = list.Count();

                rst = list.ToList();

            }
            
            return rst;
        }
    }
}
